package com.prtek.reminder.util;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import java.util.Map;

/**
 * Created by jarly on 2018/11/30.
 */

public class APPExpress {

  public static final long TIME_SECONDS = 1000;
  public static final long TIME_MINUTES = TIME_SECONDS * 60;
  public static final long TIME_HOURS = TIME_MINUTES * 60;
  public static final long TIME_DAY = TIME_HOURS * 24;
  public static final long TIME_MONTH = TIME_DAY * 31;
  public static final long TIME_YEAR = TIME_DAY * 365;
  private ExpressRunner mExpressRunner;
  private DefaultContext<String, Object> mExpressContext;

  private APPExpress() {
    mExpressRunner = new ExpressRunner();
    mExpressContext = new DefaultContext<String, Object>();
    mExpressContext.put("个小时后", TIME_HOURS);
    mExpressContext.put("分钟后", TIME_MINUTES);
    mExpressContext.put("天", TIME_MINUTES);
    mExpressContext.put("月", TIME_MONTH);
    mExpressContext.put("年", TIME_YEAR);
  }

  public long execute(String express) throws Exception {

    switch (express) {
      case "明天":
        return TIME_DAY;
      case "后天":
        return TIME_DAY * 2;
      case "明年":
        return TIME_YEAR;
    }

    express = express.replaceAll("个小时后", "*个小时后");
    express = express.replaceAll("分钟后", "*分钟后");
    express = express.replaceAll("天后", "*天");
    express = express.replaceAll("个月后", "*月");
    express = express.replaceAll("年后", "*年");

    return (long) mExpressRunner.execute(express, mExpressContext, null, true, false);
  }

  public static APPExpress getInstance() {
    return InstanceHolder.express;
  }

  public void putContext(Map<String, Long> map) {
    mExpressContext.putAll(map);
  }

  interface InstanceHolder {
    APPExpress express = new APPExpress();
  }
}
